﻿using System;
using System.Data;
using System.Data.Common;

namespace DreamCube.Framework.DataAccess.Basic
{
    [AttributeUsage(AttributeTargets.Property)]
    public class DBColumnAttribute : Attribute
    {
        #region "字段"

        private DbType dbType;

        /// <summary>
        /// 把属性/字段数据转化成对应的数据库该列的数据
        /// </summary>
        private Func<Object, Object> parseToDB;

        /// <summary>
        /// 把对应的数据库该列的数据转化回对象的属性/字段的数据
        /// </summary>
        private Func<Object, Object> deParseToObj;

        #endregion

        #region "属性"

        /// <summary>
        /// 把属性/字段数据转化成对应的数据库该列的数据
        /// </summary>
        public Func<Object, Object> ParseToDB
        { 
            get { return parseToDB; } 
        }

        /// <summary>
        /// 把对应的数据库该列的数据转化回对象的属性/字段的数据
        /// </summary>
        private Func<Object, Object> DeParseToObj
        { 
            get { return deParseToObj; } 
        }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public DbType DbType
        {
            get { return this.dbType; }
            set { this.dbType = value; }
        }

        /// <summary>
        /// 判定是否忽略默认值（对于字段是结构体的情况下，此属性非常重要）
        /// </summary>
        public Boolean IgnoreDefaultValue
        {
            get;
            set;
        }

        /// <summary>
        /// 对应的列名
        /// </summary>
        public String ColumnName
        {
            get;
            set;
        }

        /// <summary>
        /// 参数方向
        /// </summary>
        public ParameterDirection ParamDirection
        {
            get;
            set;
        }

        /// <summary>
        /// 列的数据长度
        /// </summary>
        public Int32 ColumnSize
        {
            get;
            set;
        }

        /// <summary>
        /// 标志是否是主键
        /// </summary>
        public Boolean IsPrimaryKey
        {
            get;
            set;
        }

        /// <summary>
        /// 是否自增列
        /// </summary>
        public Boolean IsIdentity
        {
            get;
            set;
        }

        #endregion

        #region "方法"

        /// <summary>
        /// </summary>
        /// <param name="columnName">数据库列名</param>
        public DBColumnAttribute(String columnName)
        {
            this.ColumnName = columnName;
        }

        public DBColumnAttribute(String columnName,
                                Func<Object, Object> parseToDBHandler,
                                Func<Object, Object> deParseToObjHandler)
        {
            this.ColumnName = columnName;
            this.parseToDB = parseToDBHandler;
            this.deParseToObj = deParseToObjHandler;
        }

        #endregion
    }
}
